package com.oa.modules.approvalsec1.controller;

import java.util.*;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oa.common.utils.*;
import com.oa.modules.approvalsec1.entity.UserBankEntity;
import com.oa.modules.approvalsec1.entity.UserSocialEntity;
import com.oa.modules.approvalsec1.service.UserBankService;
import com.oa.modules.approvalsec1.service.UserSocialService;
import com.oa.modules.sys.controller.AbstractController;
import com.oa.modules.sys.entity.SysUserEntity;
import com.oa.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.oa.modules.approvalsec1.entity.BankAccountEntity;
import com.oa.modules.approvalsec1.service.BankAccountService;
import com.oa.common.utils.R;


/**
 * 银行开户
 *
 * @author jiangwei
 * @email 785297226@qq.com
 * @date 2021-07-29 16:23:38
 */
@RestController
@RequestMapping("bank/account")
@Api(tags = "银行开户相关接口")
public class BankAccountController extends AbstractController {
    @Autowired
    private BankAccountService bankAccountService;

    @Autowired
    private UserBankService userBankService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private UserSocialService userSocialService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = bankAccountService.queryPage(params);
        if(params.get("asd")!=null&& Convert.toInt(params.get("asd")) ==1){
            List<BankAccountEntity> list = (List<BankAccountEntity>) page.getList();
            list.forEach(item->{
                item.setCallName(Base64.encode(item.getCallName()));
                item.setCallNumber(Base64.encode(item.getCallNumber()));
            });
            page.setList(list);
        }
        return R.ok().put("page", page);
    }

    /**
     * 获取数据
     */
    @GetMapping("/queryList")
    @ApiOperation(value = "获取数据")
    public R queryList(@RequestParam Map<String, Object> params) {
        List<BankAccountEntity> list = bankAccountService.queryList(params);
        for (BankAccountEntity bankAccount : list) {
            bankAccount.setBankUserName(sysUserService.getById(bankAccount.getBankUserId()).getUsername());
        }
        return R.ok().put("data", list);
    }

    /**
     * 信息
     */
    @GetMapping("/getByCompanyName")
    @ApiOperation(value = "根据公司名称获取信息")
    public com.oa.common.utils.R getByCompanyName(@RequestParam java.util.Map<String, String> params) {
        if (org.apache.commons.lang.StringUtils.isBlank(params.get("companyName"))) {
            return R.error("缺失必传参数");
        }
        BankAccountEntity bankAccount = bankAccountService.getByCompanyName(params.get("companyName"));
        if (bankAccount != null) {
            return R.ok().put("bankAccount", bankAccount);
        } else {
            return R.error("暂无数据");
        }
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "信息")
    public R info(@PathVariable("id") String id) {
        BankAccountEntity bankAccount = bankAccountService.getById(id);
        if (bankAccount != null) {
            return R.ok().put("bankAccount", bankAccount);
        } else {
            return R.error("暂无数据");
        }
    }


    @GetMapping("/aInfo")
    @ApiOperation(value = "信息")
    public R aInfo(@RequestParam("id") String id) {
        BankAccountEntity bankAccount = bankAccountService.getById(id);
        bankAccount.setCallNumber(Base64.encode(bankAccount.getCallNumber()));
        bankAccount.setCallName(Base64.encode(bankAccount.getCallName()));
        if (bankAccount != null) {
            return R.ok().put("bankAccount", bankAccount);
        } else {
            return R.error("暂无数据");
        }
    }

    /**
     * 信息
     */
    @GetMapping("/asdInfo/{id}")
    @ApiOperation(value = "信息")
    public R asdInfo(@PathVariable("id") String id) {
        BankAccountEntity bankAccount = bankAccountService.getById(id);
        bankAccount.setCallNumber(Base64.encode(bankAccount.getCallNumber()));
        bankAccount.setCallName(Base64.encode(bankAccount.getCallName()));
        if (bankAccount != null) {
            return R.ok().put("bankAccount", bankAccount);
        } else {
            return R.error("暂无数据");
        }
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public R save(@RequestBody BankAccountEntity bankAccount) {
        if (org.apache.commons.lang.StringUtils.isBlank(bankAccount.getCompanyName())) {
            return R.error("缺失必传参数企业名称");
        }
        if (org.apache.commons.lang.StringUtils.isBlank(bankAccount.getBankName())) {
            return R.error("缺失必传参数银行名称");
        }
        BankAccountEntity data = bankAccountService.getByCompanyName(bankAccount.getCompanyName());
        if (data == null) {
            String[] bankNames = bankAccount.getBankName().split(",");
            for (int i = 0; i < bankNames.length; i++) {
                List<UserBankEntity> userBank = userBankService.getByBankName(bankNames[i]);
                userBank.forEach(item -> {
                    bankAccount.setBankUserId(item.getUserid());
                    bankAccount.setBankName(item.getBankName());
                    bankAccount.setId(AppUtils.getUUID());
                    bankAccount.setCreateDate(new Date());
                    bankAccountService.save(bankAccount);
                    String response = WxLoginController.OpenAccountPush(item.getWxOpenId(), bankAccount.getCompanyName(), bankAccount.getCallName(), bankAccount.getCallNumber(), bankAccount.getSocialCreditCode(), bankAccount.getAdress());
                });
            }
            return R.ok();
        }
        return R.error("该企业已存在");
    }

    /**
     * 保存推送
     *
     * @param bankAccount
     * @return
     */
    @PostMapping("/savePush")
    public R savePush(@RequestBody BankAccountEntity bankAccount) {
        if (org.apache.commons.lang.StringUtils.isBlank(bankAccount.getCompanyName())) {
            return R.error("缺失必传参数企业名称");
        }
        if (org.apache.commons.lang.StringUtils.isBlank(bankAccount.getBankName())) {
            return R.error("缺失必传参数银行名称");
        }
        BankAccountEntity bk = bankAccountService.getById(bankAccount.getId());
        if (bk != null) {
            //手机号解密
            if(bankAccount.getCallNumber().length()!=11){
                bankAccount.setCallNumber(Base64.decodeStr(bankAccount.getCallNumber()));
            }
            bankAccountService.updateById(bankAccount);
            BankAccountEntity bankAccountEntity = bankAccountService.getById(bankAccount.getId());
            String socialType = bankAccountEntity.getSocialType();
            String[] split = socialType.split(",");
            for (String s : split) {
                System.out.println(s);
                QueryWrapper eq = new QueryWrapper<>().eq("SOCIAL_TYPE", s);
                List<UserSocialEntity> list = userSocialService.list(eq);
                if (list.size() > 0) {
                    list.forEach(item -> {
                        QueryWrapper wrapper = new QueryWrapper<>().eq("mobile", item.getUserTel());
                        SysUserEntity userEntity = sysUserService.getOne(wrapper);
                        if (userEntity != null && StringUtils.isNotEmpty(userEntity.getWxOpenId())) {
                            String response = WxLoginController.BankAccountPush(userEntity.getWxOpenId(), bankAccountEntity.getCompanyName(), bankAccountEntity.getSocialCreditCode(), bankAccountEntity.getAdress(), bankAccountEntity.getCallName(), bankAccountEntity.getTransactor());
                            JSONObject jsonObject = JSON.parseObject(response);
                            System.out.println(jsonObject);
                        }
                    });
                }
            }
            return R.ok("推送成功");
        } else {
            return R.error("未找到开户信息");
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(@RequestBody BankAccountEntity bankAccount) {

        bankAccount.setUpdateDate(new Date());
        bankAccountService.updateById(bankAccount);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public R delete(@RequestBody String[] ids) {
        bankAccountService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 根据userid查询用户的开户信息
     *
     * @param params
     * @return
     */
    @GetMapping("/newBankInfo")
    @ApiOperation(value = "查询用户的开户信息")
    public R newtUserList(@RequestParam Map<String, Object> params) {
        return R.ok().put("data", bankAccountService.newBankInfo(params));
    }

    /**
     * 根据社会类型查询开户列表
     *
     * @param params
     * @return
     */
    @GetMapping("/findSocialList")
    @ApiOperation(value = "根据社会类型查询开户列表")
    public R findBySocialList(@RequestParam Map<String, Object> params) {
        System.out.println(params.get("socialType"));
        PageUtils page = bankAccountService.socialListPage(params);
        return R.ok().put("page", page);
    }

}
